import {$http} from "@escook/request-miniprogram";

// 接口基地址
// 生产
$http.baseUrl = "https://j.kaletcg.com/lzmtest"
// 开发

// $http.baseUrl = "http://192.168.0.38:8081"

// 请求拦截
$http.beforeRequest = options => {
    const defaultHeader = {}
    defaultHeader['Content-Type'] = options.method == 'GET' ? "application/x-www-form-urlencoded" :
        "application/json"
    defaultHeader['api-access-token'] = uni.getStorageSync("token")
    options.header = {
        ...defaultHeader,
        ...options.header
    }
    return options
}


// 请求拦截
$http.beforeRequest = (options) => {
    const defaultHeader = {};
    defaultHeader["Content-Type"] =
        options.method == "GET"
            ? "application/x-www-form-urlencoded"
            : "application/json";
    defaultHeader["api-access-token"] = uni.getStorageSync("token");
    options.header = {
        ...defaultHeader,
        ...options.header,
    };
    return options;
};

// 响应拦截
$http.afterRequest = (res) => {
    if (res.data.code !== 200) {
        uni.showToast({
            title: res.data.message,
            icon: "none",
        });
        if (res.data.code === 555) {
            uni.removeStorageSync("token");
            uni.showModal({
                content: "请登录",
                success: (res) => {
                    if (res.confirm) {
                        uni.navigateTo({
                            url: "/pages/login/login",
                        });
                    } else if (res.cancel) {
                        uni.navigateBack({});
                    }
                },
            });
        }
    }
};

export default $http;
